home *** CD-ROM | disk | FTP | other *** search
- Path: news.kei.com!ub!newserve!rebecca!rpi!not-for-mail
- From: floydb1@lib104.its.rpi.edu (Barry B Floyd)
- Newsgroups: comp.lang.c++
- Subject: Re: [Q] Converting dates in MMYY to YYMMDD format
- Date: 25 Jan 1996 10:28:21 -0500
- Organization: Rensselaer Polytechnic Institute, Troy, NY.
- Message-ID: <4e87il$pa@lib104.its.rpi.edu>
- References: <4e6ms8$ad5@walrus2.walrus.com>
- NNTP-Posting-Host: lib104.its.rpi.edu
- X-newsreader: xrn 7.04-beta-11
-
-
- In article <4e6ms8$ad5@walrus2.walrus.com>, fjordao@walrus.com (Felipe Jordao) writes:
- |>
- |> Hi,
- |>
- |> I have a rather clumsy function that converts a character string in
- |> MMYY format, (such as 1195 for November 1995) into a long DDMMYY
- |> format, where DD is always the last day of the month. Hence "1282"
- |> would be converted into 821231 (a long).
- |>
- |> Does anyone know a quick way of doing this (around 10-20 lines of
- |> code)?
- |>
- |> Felipe
- |>
-
- One line of (Pseudo)code, should do ( i.e. "result =..." ), using
- a String class:
-
- main ()
- {
- while ( TRUE )
- {
- String last_days = "3128..." ;
- String date ;
- String result ;
-
- cout << "Your date (MMYY): " ;
- cin >> date ;
-
- result = date.at ( 2, 2 ) + // ~.at ( from, len )
- date.at ( 0, 2 ) +
- last_days.at (
- ( ( atoi ( (char *)date.at ( 2, 2 ) ) - 1 ) * 2 )
- , 2 ) ;
-
- cout << "YYMMDD" << result_date << endl ;
- }
- }
-
- You might check to see if the 'date' is four characters long, then
- pad to the left (maybe one "0") if is not.
-
-
- Alternatives:
-
- If you don't use a String class, (char *)'s
- should work - replacing "+" with strcat(...) etc.
- -- about three lines.
-
- Or use (char[]) to fill the result array from (char)'s
- in "date" and "last_days" ( i.e. result[0] = date[2] ;
- result[1] = date[3]) and an 'end-of-string' null.
- -- about six lines of code.
-
- Or convert "date" to (int) and div/mult it, then
- add last_days[MONTH-1], where "int last_days[0] = 31 ; etc.
- -- about three lines of code.
-
- good luck
-
- barry
- --
- +--------------------------------------------------------------------+
- | Barry B. Floyd \\\ floydb1@rpi.edu |
- | RPI Alum. '84 '87 '88 \\\ |
- +--------------------------------------------------------------------+
-